package com.hckj.jianzhijia;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.hckj.jianzhijia.listeners.OnBackClickListener;
import com.hckj.jianzhijia.model.VersionUpdate;
import com.hckj.jianzhijia.services.APKLoadingService;
import com.hckj.jianzhijia.utils.ExceptionUtils;
import com.hckj.jianzhijia.utils.MyActivityManager;
import com.hckj.jianzhijia.utils.SharedPreferencesUtil;
import com.hckj.jianzhijia.utils.ToastUtils;
import com.rey.material.app.Dialog;
import com.rey.material.app.DialogFragment;
import com.rey.material.app.SimpleDialog;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 修改密码，新版本检测，关于兼职家招兼职版，兼职猫求职端下载，退出
 */
public class WatchMoreActivity extends BaseActivity {

    @InjectView(R.id.imgv_go_back)
    ImageView imgvGoBack;
    @InjectView(R.id.ll_modify_password)
    LinearLayout llModifyPassword;
    @InjectView(R.id.ll_update_check)
    LinearLayout llUpdateCheck;
    @InjectView(R.id.ll_about_us)
    LinearLayout llAboutUs;
    @InjectView(R.id.ll_download)
    LinearLayout llDownload;
    @InjectView(R.id.btn_logout)
    Button btnLogout;


    private VersionUpdate versionUpdate;
    private SharedPreferencesUtil sharedPreferencesUtil;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_watch_more);
        ButterKnife.inject(this);
        sharedPreferencesUtil = new SharedPreferencesUtil(this);
        initEvent();
    }

    private void initEvent() {
        imgvGoBack.setOnClickListener(new OnBackClickListener(this));
        llModifyPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),MidifyPasswordActivity.class));
            }
        });

        llUpdateCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                versionUpdate();
            }
        });

        llAboutUs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), WebPageActivity.class);
                intent.putExtra("WebPageType",WebPageActivity.WEBPAGE_ABOUT_US);
                startActivity(intent);
            }
        });

        llDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), WebPageActivity.class);
                intent.putExtra("WebPageType",WebPageActivity.WEBPAGE_DOWNLOAD);
                startActivity(intent);
            }
        });

        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Dialog.Builder builder = null;
                builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {
                    @Override
                    public void onPositiveActionClicked(DialogFragment fragment) {
                        sharedPreferencesUtil.cleanUserInfo();
                        startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                        MyActivityManager.getInstance().AppExit(WatchMoreActivity.this);
                        jianzhijiaApplacation.getCompanyUser().logOut();
                        super.onPositiveActionClicked(fragment);
                    }

                    @Override
                    public void onNegativeActionClicked(DialogFragment fragment) {
                        fragment.dismiss();
                        super.onNegativeActionClicked(fragment);
                    }
                };

                ((SimpleDialog.Builder) builder).message("退出当前账号后，您的登录记录将被清空，请问您确认退出吗")
                            .positiveAction("确认")
                            .negativeAction("取消").title("退出");
                DialogFragment fragment = DialogFragment.newInstance(builder);
                fragment.show(getSupportFragmentManager(), null);




            }
        });
    }


    private void versionUpdate() {

        PackageInfo info = null;
        try {
            info = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        // 当前版本的版本号
        AVQuery<AVObject> query = new AVQuery<AVObject>("VersionUpdate");
        query.whereEqualTo("clientType", 0);
        query.orderByDescending("versionCode");
        query.whereGreaterThan("versionCode", info.versionCode);

        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if (e == null) {
                    if (list.size() != 0) {
                        versionUpdate = new VersionUpdate(list.get(0));
                        Dialog.Builder builder = null;
                        builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {
                            @Override
                            public void onPositiveActionClicked(DialogFragment fragment) {

                                if(versionUpdate.getAPK() == null){
                                    ToastUtils.showToast(getApplicationContext(),"更新失败");
                                    fragment.dismiss();
                                    return ;
                                }

                                APKLoadingService.startUpdate(WatchMoreActivity.this, versionUpdate.getAPK().getUrl());
                                super.onPositiveActionClicked(fragment);
                            }

                            @Override
                            public void onNegativeActionClicked(DialogFragment fragment) {

                                super.onNegativeActionClicked(fragment);
                            }
                        };

                        ((SimpleDialog.Builder) builder).message("更新内容：" + versionUpdate.getVersionDescribe())
                                    .positiveAction("确认")
                                    .negativeAction("取消").title("更新");
                        DialogFragment fragment = DialogFragment.newInstance(builder);
                        fragment.show(getSupportFragmentManager(), null);

                    }else{
                        ToastUtils.showToast(getApplicationContext(), "当前已是最新版本");

                    }
                } else {
                    ToastUtils.showToast(getApplicationContext(), "获取更新信息失败 " + ExceptionUtils.getExceptionInfo(e));
                }
            }
        });


    }

}
